package com.ruoyi.attendance.controller;


import com.github.pagehelper.PageInfo;
// import com.ruoyi.attendance.domain.ScheduleManagement;
import com.ruoyi.attendance.dto.ScheduleQueryDTO;
import com.ruoyi.attendance.entity.ScheduleManagement;
import com.ruoyi.attendance.mapper.ScheduleManagementMapper;
import com.ruoyi.attendance.service.ScheduleService;
import com.ruoyi.attendance.vo.ScheduleUserVO;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Slf4j
@RestController
@RequestMapping("/attendance/attendance")
public class ScheduleController extends BaseController {

    @Autowired
    private ScheduleService scheduleService;
    @Autowired
    private ScheduleManagementMapper scheduleManagementMapper;

    /**
     * 查询排班列表
     */
    @PreAuthorize("@ss.hasPermi('attendance:attendance:list')")
    @GetMapping("/list")
    public TableDataInfo list(ScheduleQueryDTO query) {
        log.info("ScheduleController.list: query={}", query);
        // startPage(); // 若依框架的分页辅助方法
        List<ScheduleUserVO> list = scheduleService.listScheduleUsers(query);
        // log.info("这是list:");
        // log.info("ScheduleController.list: list={}", list);

        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(list);

        rspData.setTotal(scheduleManagementMapper.countValidUsers(query));

        return rspData;
    }

    // 批量插入排班记录
    @PostMapping("/generate")
    public AjaxResult generateDefaultSchedules(@RequestParam(required = false) Integer days) {
        log.info("ScheduleController.generateDefaultSchedules: days={}", days);
        return AjaxResult.success(scheduleService.generateDefaultSchedules(days));
    }


    /**
     * 修改员工排班管理
     */
    @PreAuthorize("@ss.hasPermi('attendance:attendance:edit')")
    @Log(title = "员工排班管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ScheduleManagement scheduleManagement)
    {
        log.info("ScheduleController.edit: scheduleManagement={}", scheduleManagement);

        return toAjax(scheduleService.updateScheduleManagement(scheduleManagement));
    }


}